/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.profile_manager.profile;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Social extends Profile {
	public int children, ethnicity, religion, polView, sexOri, smoking,
			drinking, pets;

	public boolean[] humor = new boolean[6], fashion = new boolean[11],
			living = new boolean[8]; // TODO boring stuff

	public String hometown, webpage, aboutMe, passions, sports, activities,
			books, music, tvShows, movies, cussines;

	public String toXML() {
		StringBuilder output = new StringBuilder();
		output
				.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources>");
		output.append("<item name=\"cuisines\">" + cussines + "</item>");
		output.append("<item name=\"movies\">" + movies + "</item>");
		output.append("<item name=\"tvShows\">" + tvShows + "</item>");
		output.append("<item name=\"music\">" + music + "</item>");
		output.append("<item name=\"books\">" + books + "</item>");
		output.append("<item name=\"activities\">" + activities + "</item>");
		output.append("<item name=\"sports\">" + sports + "</item>");
		output.append("<item name=\"passions\">" + passions + "</item>");
		output.append("<item name=\"aboutMe\">" + aboutMe + "</item>");
		output.append("<item name=\"webpage\">" + webpage + "</item>");
		output.append("<item name=\"hometown\">" + hometown + "</item>");
		output.append("<item name=\"pets\">" + pets + "</item>");
		output.append("<item name=\"drinking\">" + drinking + "</item>");
		output.append("<item name=\"smoking\">" + smoking + "</item>");
		output.append("<item name=\"sexOri\">" + sexOri + "</item>");
		output.append("<item name=\"polView\">" + polView + "</item>");
		output.append("<item name=\"religion\">" + religion + "</item>");
		output.append("<item name=\"ethnicity\">" + ethnicity + "</item>");
		output.append("<item name=\"children\">" + children + "</item>");
		output.append("</resources>");
		
		
	/*	StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i< output.length(); i++){
			char c;
			if((c = output.charAt(i))==' '){
				sb.append("%20");
			}else{
				sb.append(c);
			}
		}*/
		return output.toString();
	}

	public void fromXML(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	class MyHandler extends DefaultHandler {
		String lastElement;

		boolean begin;

		public void endElement(String uri, String localName, String qName) {
			begin = false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin = true;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
			}
		}

		public void characters(char[] ch, int start, int length) {
			String s = new String(ch, start, length);
			if(s.compareTo("null")==0)return;
			if (lastElement == null)
				return;
			if (!begin)
				return;
			if (lastElement.compareTo("children") == 0) {
				Social.this.children = Integer.parseInt(s);
			} else if (lastElement.compareTo("ethnicity") == 0) {
				Social.this.ethnicity = Integer.parseInt(s);
			} else if (lastElement.compareTo("religion") == 0) {
				Social.this.religion = Integer.parseInt(s);
			} else if (lastElement.compareTo("polView") == 0) {
				Social.this.polView = Integer.parseInt(s);
			} else if (lastElement.compareTo("sexOri") == 0) {
				Social.this.sexOri = Integer.parseInt(s);
			} else if (lastElement.compareTo("smoking") == 0) {
				Social.this.smoking = Integer.parseInt(s);
			} else if (lastElement.compareTo("drinking") == 0) {
				Social.this.drinking = Integer.parseInt(s);
			} else if (lastElement.compareTo("pets") == 0) {
				Social.this.pets = Integer.parseInt(s);
			} else if (lastElement.compareTo("hometown") == 0) {
				Social.this.hometown = s;
			} else if (lastElement.compareTo("webpage") == 0) {
				Social.this.webpage = s;
			} else if (lastElement.compareTo("aboutMe") == 0) {
				Social.this.aboutMe = s;
			} else if (lastElement.compareTo("passions") == 0) {
				Social.this.passions = s;
			} else if (lastElement.compareTo("sports") == 0) {
				Social.this.sports = s;
			} else if (lastElement.compareTo("activities") == 0) {
				Social.this.activities = s;
			} else if (lastElement.compareTo("books") == 0) {
				Social.this.books = s;
			} else if (lastElement.compareTo("music") == 0) {
				Social.this.music = s;
			} else if (lastElement.compareTo("tvShows") == 0) {
				Social.this.tvShows = s;
			} else if (lastElement.compareTo("movies") == 0) {
				Social.this.movies = s;
			} else if (lastElement.compareTo("cuisines") == 0) {
				Social.this.cussines = s;
			}
		}
	}
}
